/**
 * 
 */
package com.bj58.data.hadoop.args;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义Input数据，可以对应批量多格式的内容
 * @author zhangpeng
 */
public class Input {
	
	public static final String YYYYMMDD = "yyyyMMdd";
	
	public static Set<String> getInputPath(String inputs)
	{
		return getInputPath(inputs,YYYYMMDD);
	}
	
	public static Set<String> getInputPath(String inputs,String timeFormat)
	{
		Set<String> inputPaths = new HashSet<String>();
		SimpleDateFormat dateFormatter = new SimpleDateFormat(timeFormat);
		
		Calendar calcurrent = Calendar.getInstance();
		calcurrent.add(Calendar.DATE, -1);
		calcurrent.set(Calendar.SECOND, 0);
		calcurrent.set(Calendar.MINUTE, 0);
		calcurrent.set(Calendar.HOUR_OF_DAY, 0);
		Date dataTime = calcurrent.getTime();
		String currentDate = dateFormatter.format(dataTime);
		String tempPaths = inputs.replace(timeFormat, currentDate);	
		Pattern datesPattern = Pattern.compile("\\{\\S+\\}"); /*正则表达式*/
		String pathsTemplete = tempPaths.replaceAll("\\{\\S+\\}", "{0}");
		Matcher datesMatcher = datesPattern.matcher(tempPaths); /*输入字符串*/
		while (datesMatcher.find()) {		
			String tempDates = datesMatcher.group().substring(1, datesMatcher.group().length()-1);
			String[] aryDates = tempDates.split("\\|");
			for(String aryDate: aryDates)
			{
				if(aryDate.indexOf("-") > -1)
				{
					String[] areaTime = aryDate.split("-");
					Integer startTime =  Integer.valueOf(areaTime[0]);
					Integer endTime =  Integer.valueOf(areaTime[1]);
					while(startTime<=endTime)
					{
						inputPaths.add(pathsTemplete.replace("{0}", startTime.toString()));
						startTime++;
					}
				}
				else
				{
					inputPaths.add(pathsTemplete.replace("{0}", aryDate.toString()));
				}
			}
		}
		return inputPaths;
	}
	
	public static void main(String[] args) {
		Set<String> inputPath = getInputPath("/dsap/rawdata/infostat/${yyyyMMdd}");
		for(String path:inputPath)
		{
			System.out.println(path);
		}
	}
}
